/*
 * @Description: Jishupu-Admin
 * @Author: 技术铺<jishupu@qq.com>
 * @Date: 2021-11-17
 */
import store from '@/store'
import { getToken } from '@/utils/auth' // get token from cookie

export default class {
  constructor() {
    this.data = []
  }

  /**
   * 获取字典列表
   * @returns {Array}
   */
  getList() {
    const hasToken = getToken()
    if (hasToken) {
      if (store.getters.dictionaryConfig.length === 0) {
        this.data = store.dispatch('dictionaryConfig/addConfig', null, { root: true }).catch((e) => {})
      } else {
        this.data = store.getters.dictionaryConfig
      }
    }
    return this.data
  }

  /**
   * 根据字典（英文）标识获取字典信息
   * @param {String} id_name 英文标识
   * @returns {Object}
   */
  getDetail(id_name) {
    const result = this.getList().find(item => {
      return item.id_name === id_name
    })
    return result || null
  }

  /**
   * 获取字典名称
   * @param {String} id_name
   * @returns {String}
   */
  getName(id_name) {
    const getDetail = this.getDetail(id_name)
    return getDetail ? getDetail.name : ''
  }

  /**
   * 获取字典的选项列表
   * @param {String} id_name
   * @returns {Array}
   */
  getItems(id_name) {
    const getDetail = this.getDetail(id_name)
    return getDetail ? getDetail.dictionaryItems : []
  }

  /**
   * 获取字典选项列表中的指定项
   * @param {String} id_name
   * @param {String} dictionaryItems_value 选项值
   * @returns {Object}
   */
  getOneItem(id_name, dictionaryItems_value) {
    const getItems = this.getItems(id_name)
    const result = getItems.find(i => {
      return String(i.value) === String(dictionaryItems_value)
    })
    return result || null
  }

  /**
   * 获取字典选项名称
   * @param {String} id_name
   * @param {Number} dictionaryItems_value
   * @returns {String}
   */
  getItemName(id_name, dictionaryItems_value) {
    const getOneItem = this.getOneItem(id_name, dictionaryItems_value)
    return getOneItem ? (getOneItem.name || '') : ''
  }

  /**
   * 获取字典选项描述
   * @param {String} id_name
   * @param {Number} dictionaryItems_value
   * @returns {String}
   */
  getItemDescription(id_name, dictionaryItems_value) {
    const getOneItem = this.getOneItem(id_name, dictionaryItems_value)
    return getOneItem ? (getOneItem.description || '') : ''
  }

  /**
   * 获取选项值数组集合
   * @param {String} id_name
   * @returns {Array}
   */
  getItemsValuesArray(id_name) {
    const getItems = this.getItems(id_name)
    if (!getItems) {
      return []
    }
    const result = getItems.map((item) => {
      return item.value || ''
    })

    return result || []
  }

  /**
   * 获取选项值字符串集合
   * @param {String} id_name
   * @param {String} join 拼接方式 默认英文逗号
   * @returns {String}
   */
  getItemsValuesString(id_name, join) {
    if (!join) join = ','
    const getItemsValuesArray = this.getItemsValuesArray(id_name)
    return getItemsValuesArray ? getItemsValuesArray.join(join) : ''
  }

  /**
   * 判断选项值是否存在选项值列表中
   * @param {String} id_name
   * @param {String} value
   * @returns {Boolean}
   */
  valueExist(id_name, value) {
    const getItemsValuesArray = this.getItemsValuesArray(id_name)
    return getItemsValuesArray.indexOf(String(value)) > -1
  }

  /**
   * 获取选项描述数组集合
   * @param {String} id_name
   * @returns {Array}
   */
  getItemsDescriptionsArray(id_name) {
    const getItems = this.getItems(id_name)
    if (!getItems) {
      return []
    }
    const result = getItems.map((item) => {
      return item.description || ''
    })
    return result || []
  }

  /**
   * 判断描述是否存在描述列表中
   * @param {String} id_name
   * @param {String} description 选项描述
   * @returns {Boolean}
   */
  descriptionExist(id_name, description) {
    const getItemsDescriptionsArray = this.getItemsDescriptionsArray(id_name)
    return getItemsDescriptionsArray.indexOf(String(description)) > -1
  }

  /**
   * 按照描述分组获取选项值数组集合
   * 场景：根据描述image从附件类型字典中获取图片附件
   * @param {String} id_name
   * @param {String} description 选项描述
   * @returns {Array}
   */
  getItemsValuesArrayGroupByDesc(id_name, description) {
    const getItems = this.getItems(id_name)
    if (!getItems || !description) {
      return []
    }
    const result = getItems.filter((item) => {
      return description === item.description
    }).map((item) => {
      return item.value || ''
    })
    return result || []
  }
}
